package com.feige.helper;

import com.feige.pojo.Dept;
import com.feige.pojo.vo.BaseDeptVo;

import java.util.ArrayList;
import java.util.List;

public class DeptHelper {

    /**
     *递归建部门下拉列表
     * @param depts
     * @return
     */
    public static List<BaseDeptVo> build1(List<BaseDeptVo> depts){
        List<BaseDeptVo> deptList = new ArrayList<>();
        for (BaseDeptVo dept : depts) {
            if (0 == dept.getPid()) {
                deptList.add(findChildren1(dept,depts));
            }
        }
        return deptList;
    }


    /**
     * 查找子节点
     * @param rootNode
     * @param depts
     * @return
     */
    private static BaseDeptVo findChildren1(BaseDeptVo rootNode, List<BaseDeptVo> depts){
        rootNode.setChildren(new ArrayList<>());
        for (BaseDeptVo dept : depts) {
            if (rootNode.getId().equals(dept.getPid())) {
                if (rootNode.getChildren() == null){
                    rootNode.setChildren(new ArrayList<>());
                }
                rootNode.getChildren().add(findChildren1(dept,depts));
            }
        }
        return rootNode;
    }

    /**
     *递归建菜单
     * @param depts
     * @return
     */
    public static List<Dept> build2(List<Dept> depts){
        List<Dept> deptList = new ArrayList<>();
        for (Dept dept : depts) {
            if (0 == dept.getPid()) {
                deptList.add(findChildren2(dept,depts));
            }
        }
        return deptList;
    }


    /**
     * 查找子节点
     * @param rootNode
     * @param depts
     * @return
     */
    private static Dept findChildren2(Dept rootNode, List<Dept> depts){
        rootNode.setChildren(new ArrayList<>());
        for (Dept dept : depts) {
            if (rootNode.getId().equals(dept.getPid())) {
                if (rootNode.getChildren() == null){
                    rootNode.setChildren(new ArrayList<>());
                }
                rootNode.getChildren().add(findChildren2(dept,depts));
            }
        }
        return rootNode;
    }
}
